*******************************************************************************
* Specify city and years; load data
*******************************************************************************

local city = "fort_worth"

local y1 2009
local y2 2014
local y3 2020

use dataset_`city'_cross, clear

*******************************************************************************
* Rescale explanatory variables
*******************************************************************************

foreach x in pctnhblack distancem logncrimeincidentspcp logpropertyvalues {
	su `x'
	replace `x' = (`x'- r(min)) / (r(max) - r(min))
}
replace distancem = -distancem + 1

*******************************************************************************
* Time interactions
*******************************************************************************

gen pctnhblackXy2 = pctnhblack*year_2
gen pctnhblackX2020 = pctnhblack*year_3
gen distanceXy2 = distancem*year_2
gen distanceX2020 = distancem*year_3
gen ncrimeXy2 = logncrimeincidentspcp*year_2
gen ncrimeX2020 = logncrimeincidentspcp*year_3
gen propertyvaluesXy2 = logpropertyvalues*year_2
gen propertyvaluesX2020 = logpropertyvalues*year_3

label variable pctnhblackXy2 "Share Black X 2014"
label variable pctnhblackX2020 "Share Black X 2020"
label variable distanceXy2 "Proximity to Shooting X 2014"
label variable distanceX2020 "Proximity to Shooting X 2020"
label variable ncrimeXy2 "Crime X 2014"
label variable ncrimeX2020 "Crime X 2020"
label variable propertyvaluesXy2 "Property Values X 2014"
label variable propertyvaluesX2020 "Property Values X 2020"

*******************************************************************************
* Do the regressions
*******************************************************************************

eststo clear

eststo: reg pctyes pctnhblack pctnhblackXy2 pctnhblackX2020 ///
	distancem distanceXy2 distanceX2020 ///
	logncrimeincidentspcp ncrimeXy2 ncrimeX2020 ///
	logpropertyvalues propertyvaluesXy2 propertyvaluesX2020 ///
	year_2 year_3 x_c y_c ///
	[aw=totalvotes], robust 
estadd local yfe = "X"
estadd local wtvote = "X"
estadd local clvote = ""
estadd local latlon = "X"
estadd local myse = "Robust"
	
foreach l in 1 2 5 {
	eststo: acreg pctyes pctnhblack pctnhblackXy2 pctnhblackX2020 ///
		distancem distanceXy2 distanceX2020 ///
		logncrimeincidentspcp ncrimeXy2 ncrimeX2020 ///
		logpropertyvalues propertyvaluesXy2 propertyvaluesX2020 ///
		x_c y_c year_2 year_3 ltotalvotes, ///
		spatial latitude(y_c) longitude(x_c) distcutoff(`l') ///
		hac id(precinctyear) time(year) lagcutoff(0)
	estadd local slag = `l'
	estadd local yfe = "X"
	estadd local wtvote = ""
	estadd local clvote = "X"
	estadd local latlon = "X"
	estadd local myse = "HAC"
}

*******************************************************************************
* Output table
*******************************************************************************

#delimit;

esttab
	using "_output/table A17.tex"
	,
		replace
		booktabs
		label
		nomtitles
		collabels(none)
		drop(*_c ltotal* year_2 year_3 _cons)
		cells(b(fmt(3) star) se(par))
		width(\linewidth)
		stats(yfe wtvote clvote latlon myse slag N, 
			label(
				"Year Fixed Effects"
				"Weight by Total Votes Cast"
				"Control for Log(Total Votes Cast)"
				"Control for Latitude \& Longitude"
				"Standard Errors"
				"Distance Cutoff (Miles)"
				"Number of Precincts" 
			)
			fmt(%20.0f %20.0f))
		compress
		alignment(l)	
		gap
		;
			
#delimit cr
	
*******************************************************************************
* End
*******************************************************************************
